import sqlite3
##@author Alessio Di Pietro
#@author Irene Scionti
#@brief sqlite3 db API
class sqliteDB(object):
    '''
    Manage sqlite3 database
    '''
    ## @param dbname name of the file containing the db (optional)
    def __init__(self,dbname=None):
        '''
        Constructor
        '''
        self.__dbname=dbname;

    ## @brief Sets db name 
    #@param dbname name of the file containing the db (optional)
    def setDBname(self,dbname):
        self.__dbname=dbname;
    ## @brief Open db connection
    # @return a db cursor
    def open(self):
        '''
        Open the connection to the DB file
        '''
        try:
            self.__connection=sqlite3.connect(self.__dbname)
            self.__cursor=self.__connection.cursor();
            return self.__cursor;
        except sqlite3.Error, e:
            print "An error occurred:", e.args[0]
    ## @brief Close db connection
    def close(self):
        '''
        close the connection to the DB file
        '''
        try:
            self.__connection.close()
        except sqlite3.Error, e:
            print "An error occurred:", e.args[0]
    ## @brief Commits a series of dml queries
    def commit(self):
        self.__connection.commit()
    ## @brief Execute a query
    # @param dml_query the query
    def execute(self,dml_query):
        '''
        execute an update, insert or delete query
        '''
        try:
            self.__cursor.execute(dml_query)
            self.__connection.commit()

        except sqlite3.Error, e:
            print "An error occurred:", e.args[0]
